# list1 = [24, 4, 6, 56, 7, 8]
#
# list2=[]
# sum=0
# def x():
#     for item in list1:
#         if item>10:
#             list2.append(item)
#         print(list2)
#         break
#
# def y():
#     for item in list1:
#         if item>50:
#             return item
#     return None
#
# if __name__ == '__main__':
#     x()
#     y()

"""
练习1：
需求：
定义函数，在列表中查找第一个奇数
定义函数，在列表中查找第一个能被3或5整除的数字
步骤：
    -- 根据需求，写出函数。
    -- 因为主体逻辑相同,核心算法不同.
所以使用函数式编程思想(分、隔、做)
创建通用函数find_single
    -- 在当前模块中调用
"""
# list1 = [43, 54, 54, 56, 65, 67]
#
#
# # 第一步：定义回调函数
# def condition01(item):
#     return item % 2 != 0
#
#
# def condition02(item):
#     return item % 3 == 0 or item % 5 == 0
#
#
# # 第二步：定义高阶函数
# def find_single(condition):
#     for num in list1:
#         if condition(num):
#             return num
#     return None
#
#
# # 第三步：调用高阶函数
# print(find_single(condition01))
# print(find_single(condition02))

"""
练习2：
需求：
定义函数，在员工列表中查找所有部门是9001的员工
定义函数，在员工列表中查找所有姓名是2个字的员工
步骤：
    -- 根据需求，写出函数。
    -- 因为主体逻辑相同,核心算法不同.
       所以使用函数式编程思想(分、隔、做)
       创建通用函数find_all
   -- 在当前模块中调用
"""

# class Employee:
#     def __init__(self, eid, did, name, money):
#         self.eid = eid  # 员工编号
#         self.did = did  # 部门编号
#         self.name = name
#         self.money = money
#
#     def __repr__(self):
#         return f'{self.eid} {self.did} {self.name} {self.money}'
#
#
# list_employees = [
#     Employee(1001, 9002, "师父", 60000),
#     Employee(1002, 9001, "孙悟空", 50000),
#     Employee(1003, 9002, "猪八戒", 20000),
#     Employee(1004, 9001, "沙僧", 30000),
#     Employee(1005, 9001, "小白龙", 15000),
# ]
# emp2=min(list_employees, key=lambda x: x.money)
# print(emp2)


# def condition01(employee):
#     return max(employee.money)
#
# def condition02(employee):
#     return sum(employee.money)
#
# def find_all(condition):
#     for num in list_employees:
#         if condition(num):
#             return num
#     return None
#
# print(find_all(condition01))
# print(find_all(condition02))


#
#
# # 第一步：定义回调函数
# def condition01(item):
#     return item.did == 9001
#
#
# def condition02(item):
#     return len(item.name) == 2
#
#
# # 第二步：定义高阶函数
# def find_all(condition):
#     for num in list_employees:
#         if condition(num):
#             return num
#     return None
#
#
# print(find_all(condition01))
# print(find_all(condition02))


# def fun1():
#     print("Hello World")
#
#
# fun1()
#
# fun3 = lambda p1, p2: p1 > p2
# print(fun3(8, 5))

"""
练习：
-- 在商品列表，获取所有名称与单价
-- 在商品列表中，获取所有单价小于10000的商品
-- 对商品列表，根据单价进行降序排列
-- 获取元组中长度最大的列表  ([1,1],[2,2,2],[3,3,3])
"""

# class Commodity:
#     def __init__(self, cid=0, name="", price=0):
#         self.cid = cid
#         self.name = name
#         self.price = price
#
#     def __repr__(self):
#         return f'Commodity(cid={self.cid}, name={self.name}, price={self.price})'
# list_commodity_infos = [
#     Commodity(1001, "屠龙刀", 10000),
#     Commodity(1002, "倚天剑", 10000),
#     Commodity(1003, "金箍棒", 52100),
#     Commodity(1004, "口罩", 20),
#     Commodity(1005, "酒精", 30),
# ]
# for name,price in map(lambda x: (x.name,x.price), list_commodity_infos):
#     print(name, price)
# print('=' * 30)
# for item in  filter(lambda x:x.price < 10000,list_commodity_infos):
#     print(item)
# print('=' * 30)
# for item in  sorted(list_commodity_infos, key=lambda x: x.price,reverse=True):
#     print(item)
# print('=' * 30)
# i=([1,1],[2,2,2],[3,3,3])
# for item in  filter(lambda x:len(x),i):
#     print(item)


"""
闭包练习
在不改变添插入和删除函数的代码，增加功能验证
"""

# def insert():
#     print('添加')
#
# def delete():
#     print('删除')
#
# def function(func):
#     def wrapper():
#         print('功能验证')
#         func()
#     return wrapper
#
# insert=function(insert)
# insert()

"""
练习1：不改变插入函数与删除函数代码，为其增加验证权限的功能

"""

# def verify_permissions(func):
#     def wrapper(*args, **kwargs):
#         print('验证权限')
#         res = func(*args)
#         return res
#
#     return wrapper
#
#
# @verify_permissions
# def delete():
#     print("删除")
#
#
# @verify_permissions
# def insert():
#     print("插入")
#
#
# delete()
# insert()

# nums = [10, 20, 30, 40]
# list01 = []
# for item in map(lambda x: x, nums):
#     list01.append("Num:" + str(item))
# print(list01)
#
#
# def repeat(times):
#     def decorator(func):
#         def wrapper():
#             for i in range(times):
#                 func()
#
#         return wrapper
#
#     return decorator
#
#
# @repeat(3)
# def greet():
#     print("Hello!")
#
#
# greet()
employees = [
    {"name": "Alice", "salary": 5000},
    {"name": "Bob", "salary": 6000},
    {"name": "Cathy", "salary": 4500}
]

for name in map(lambda x: x["name"], filter(lambda x: x["salary"] > 4800, employees)):
    print(name)
for nowSalary in map(lambda x: x["salary"] * 1.1, employees):
    print(nowSalary)
for nowEmployees in sorted(employees, key=lambda x: x["salary"], reverse=True):
    print(nowEmployees)
